package com.tlibrary.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.tlibrary.service.TeacherService;
import com.tlibrary.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author：wcc
 * @Description：
 * @Date：Create in: 2019/2/15--17:04
 */
@Controller
@RequestMapping("/teacher")
public class teacherController {

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/inputmul")
    public @ResponseBody
    Object inputmul(HttpServletRequest request, HttpServletResponse response){
        try {
            teacherService.inputmul(request,response);
        }catch (Exception e){
            e.printStackTrace();
            return "fail";
        }
        return "success";
    }

    @RequestMapping("/findmul")
    public void  findmul(HttpServletRequest request, HttpServletResponse response){
        HashMap map=new HashMap();
        map.put("id",request.getParameter("id"));
        HttpSession session=request.getSession();
//        String userid = session.getAttribute("id").toString();
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        Integer pageNum = Integer.valueOf(request.getParameter("page"));
        Integer pageSize = Integer.valueOf(request.getParameter("rows"));
        map.put("userid",userid);
        map.put("col",request.getParameter("col"));
        map.put("dept",request.getParameter("dept"));
        map.put("couser",request.getParameter("couser"));
        map.put("nd",request.getParameter("nd"));
        HashMap list=teacherService.findmul(pageNum,pageSize,map);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/findteacherCouser")
    public void findteacherCouser(HttpServletRequest request, HttpServletResponse response){

        HashMap list=teacherService.findCouser(request,response);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);

    }
    @RequestMapping("/addCouser")
    public void addCouser(HttpServletRequest request, HttpServletResponse response) {
        //        HttpSession session=request.getSession();
//        String userid = session.getAttribute("id").toString();
        String userid="f9876a64-255c-4f05-837a-8a837477ec33";
        String deptid = request.getParameter("deptid");
        String couser = request.getParameter("couser");
        HashMap map=new HashMap();
        map.put("ID",UUID.randomUUID().toString());
        map.put("deptid",deptid);
        map.put("couser",couser);
        map.put("userid",userid);
        map.put("time" ,new Date());
        String msg="添加成功";
        try {
            teacherService.addcouser(map);

        } catch (Exception e) {
            e.printStackTrace();
            msg="添加失败";
        }

        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);

    }
    @RequestMapping("/editCouser")
    public void editCouser(HttpServletRequest request, HttpServletResponse response) {
        //        HttpSession session=request.getSession();
//        String userid = session.getAttribute("id").toString();
        String userid="f9876a64-255c-4f05-837a-8a837477ec33";
        String deptid = request.getParameter("deptid");
        String couser = request.getParameter("couser");
        String id = request.getParameter("id");
        HashMap map=new HashMap();
        map.put("userid",userid);
        map.put("deptid",deptid);
        map.put("couser",couser);
        map.put("id",id);
        map.put("time",new Date());
        String msg = "修改成功";

        try {
            teacherService.editcouser(map);
        } catch (Exception e) {
            e.printStackTrace();
            msg="修改失败";
        }
        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }
    @RequestMapping("/delCouser")
    public void delCouser(HttpServletRequest request, HttpServletResponse response){
        String datas = request.getParameter("datas");
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        JSONArray array = JSONObject.parseArray(datas);
        String msg = "删除成功";
        try {
            teacherService.delUser(array);
        } catch (Exception e) {
            e.printStackTrace();
            msg="删除失败";
        }
        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }
    @RequestMapping("/updatemulandlab")
    public void updatemulandlab(HttpServletRequest request, HttpServletResponse response){
        HashMap hashMap=new HashMap();
        hashMap = JSONObject.parseObject(request.getParameter("datas"), hashMap.getClass());
        String msg="成功";
        try {
            teacherService.updatemulAndLab(hashMap);
        } catch (Exception e) {
            e.printStackTrace();
            msg="失败";
        }

        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/savesubquestion")
    public void savesubquestion(HttpServletRequest request, HttpServletResponse response){
        HashMap hashMap=new HashMap();
        hashMap = JSONObject.parseObject(request.getParameter("datas"), hashMap.getClass());
        String msg="成功";
        try {
            teacherService.addsubquestion(hashMap);
        } catch (Exception e) {
            e.printStackTrace();
            msg="失败";
        }
        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/findFAndTF")
    public  void findFAndTF (HttpServletRequest request, HttpServletResponse response){
        //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        HashMap hashMap=new HashMap();
        hashMap.put("lx",request.getParameter("lx"));
        hashMap.put("col",request.getParameter("col"));
        hashMap.put("dept",request.getParameter("dept"));
        hashMap.put("couser",request.getParameter("couser"));
        hashMap.put("nd",request.getParameter("nd"));
        hashMap.put("userid",userid);
        Integer pageNum = Integer.valueOf(request.getParameter("page"));
        Integer pageSize = Integer.valueOf(request.getParameter("rows"));
        HashMap list=teacherService.findFAndTF(hashMap,pageNum,pageSize);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/delmul")
    public  void  delmul (HttpServletRequest request, HttpServletResponse response){
        String datas = request.getParameter("datas");
        JSONArray array = JSONObject.parseArray(datas);
        String msg = "删除成功";
        try {
            teacherService.delMul(array);
        } catch (Exception e) {
            e.printStackTrace();
            msg="删除失败";
        }
        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/delfandtf")
    public  void  delfandtf (HttpServletRequest request, HttpServletResponse response){
        String datas = request.getParameter("datas");
        JSONArray array = JSONObject.parseArray(datas);
        String msg = "删除成功";
        try {
            teacherService.delfandtf(array);
        } catch (Exception e) {
            e.printStackTrace();
            msg="删除失败";
        }
        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/updatetfandlab")
    public void updatetfandlab(HttpServletRequest request, HttpServletResponse response){
        HashMap hashMap=new HashMap();
        hashMap = JSONObject.parseObject(request.getParameter("datas"), hashMap.getClass());
        String msg="成功";
        try {
            teacherService.updatetfandlab(hashMap);
        } catch (Exception e) {
            e.printStackTrace();
            msg="失败";
        }

        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/findans")
    public  void findans (HttpServletRequest request, HttpServletResponse response){
        //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        HashMap hashMap=new HashMap();
        hashMap.put("col",request.getParameter("col"));
        hashMap.put("dept",request.getParameter("dept"));
        hashMap.put("couser",request.getParameter("couser"));
        hashMap.put("nd",request.getParameter("nd"));
        hashMap.put("userid",userid);
        Integer pageNum = Integer.valueOf(request.getParameter("page"));
        Integer pageSize = Integer.valueOf(request.getParameter("rows"));
        HashMap list=teacherService.findANS(hashMap,pageNum,pageSize);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }


    @RequestMapping("/delANS")
    public  void  delANS (HttpServletRequest request, HttpServletResponse response){
        String datas = request.getParameter("datas");
        JSONArray array = JSONObject.parseArray(datas);
        String msg = "删除成功";
        try {
            teacherService.delANS(array);
        } catch (Exception e) {
            e.printStackTrace();
            msg="删除失败";
        }
        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/updatesubandlab")
    public void updatesubandlab(HttpServletRequest request, HttpServletResponse response){
        HashMap hashMap=new HashMap();
        hashMap = JSONObject.parseObject(request.getParameter("datas"), hashMap.getClass());
        String msg="成功";
        try {
            teacherService.updatesubandlab(hashMap);
        } catch (Exception e) {
            e.printStackTrace();
            msg="失败";
        }

        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/getmulmsg")
    public void getmulmsg(HttpServletRequest request, HttpServletResponse response){
        HashMap hashMap=new HashMap();
        hashMap.put("id",request.getParameter("id"));
        //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        hashMap.put("userid",userid);
        Integer pageNum = 1;
        Integer pageSize = 1;
        HashMap list=teacherService.findmul(pageNum,pageSize,hashMap);
        List rows = (List) list.get("rows");
        Map o = (Map) rows.get(0);
        Gson gson = new Gson();
        String json = gson.toJson(o);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/getftandfmsg")
    public void getftandfmsg(HttpServletRequest request, HttpServletResponse response){
        //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        HashMap hashMap=new HashMap();
        hashMap.put("id",request.getParameter("id"));
        hashMap.put("userid",userid);
        Integer pageNum = 1;
        Integer pageSize = 1;
        HashMap list=teacherService.findFAndTF(hashMap,pageNum,pageSize);
        List rows = (List) list.get("rows");
        Map o = (Map) rows.get(0);
        Gson gson = new Gson();
        String json = gson.toJson(o);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }

    @RequestMapping("/findsubmsg")
    public  void findsubmsg (HttpServletRequest request, HttpServletResponse response){
        //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        HashMap hashMap=new HashMap();
        hashMap.put("id",request.getParameter("id"));
        hashMap.put("userid",userid);
        Integer pageNum = 1;
        Integer pageSize = 1;
        HashMap list=teacherService.findANS(hashMap,pageNum,pageSize);
        List rows = (List) list.get("rows");
        Map o = (Map) rows.get(0);
        Gson gson = new Gson();
        String json = gson.toJson(o);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }
    @RequestMapping("/save_pwd")
    public  void save_pwd (HttpServletRequest request, HttpServletResponse response){
        //        String userid = session.getAttribute("id").toString();
//               HttpSession session=request.getSession();
        String userid = "f9876a64-255c-4f05-837a-8a837477ec33";
        HashMap hashMap=new HashMap();
        hashMap.put("id",request.getParameter("id"));
        hashMap.put("old",request.getParameter("old"));
        hashMap.put("new",request.getParameter("new"));
        String msg="保存成功";
        try {
            teacherService.updatepwd(hashMap);
        } catch (Exception e) {
            msg="保存失败";
            e.printStackTrace();
        }
        Gson gson = new Gson();
        String json = gson.toJson(msg);
        //将数据发送到前端
        System.out.println(json);
        ResponseUtil.toHtml(response, json);
    }
}
